package com.skyward.android.androiddemo2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.skyward.android.androiddemo2.bean.StrokenLineBean;

import java.util.List;

/**
 * Created by skyward on 2017/4/10.
 * email:
 */

public class TwentyFourHourView extends View {

    private List<StrokenLineBean> mStrokenLineBeanList;
    private Paint mPaint;
    private Paint mPaint2;
    private Paint mPaint3;

    private static final int ITEM_SIZE = 24;  //24小时
    private static final int ITEM_WIDTH = 140; //每个Item的宽度
    private static final int MARGIN_LEFT_ITEM = 100; //左边预留宽度
    private static final int MARGIN_RIGHT_ITEM = 100; //右边预留宽度

    private int mHeight, mWidth;




    public TwentyFourHourView(Context context) {
        super(context);
        init();
    }

    public TwentyFourHourView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TwentyFourHourView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    private void init() {
        mWidth = MARGIN_LEFT_ITEM + MARGIN_RIGHT_ITEM + ITEM_SIZE * ITEM_WIDTH;
        mHeight = 600; //暂时先写死

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.GRAY);
        mPaint.setTextSize(40);

        mPaint2  = new Paint();
        mPaint2.setAntiAlias(true);
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setStrokeWidth(3);
        mPaint2.setColor(Color.parseColor("#24C3F1"));

        mPaint3 = new Paint();
        mPaint3.setAntiAlias(true);
        mPaint3.setStyle(Paint.Style.FILL);
        mPaint3.setStrokeWidth(2);
        mPaint3.setTextSize(20);
        mPaint3.setColor(Color.parseColor("#24C3F1"));

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int y = (mHeight-40)/70;
        int zeroXY = y*50;

        canvas.drawLine(0,mHeight-40,mWidth,mHeight-40,mPaint);

        canvas.drawPoint(10,zeroXY,mPaint);
        for (int i = 0; i < mStrokenLineBeanList.size(); i++) {
            canvas.drawText(mStrokenLineBeanList.get(i).getHours(),MARGIN_LEFT_ITEM+ITEM_WIDTH*i,mHeight-2,mPaint);
            int temperature = mStrokenLineBeanList.get(i).getTemperature();
            if(temperature >0){
                canvas.drawCircle(MARGIN_LEFT_ITEM+ITEM_WIDTH*i,zeroXY - temperature*y,5,mPaint2);
            }else {
                canvas.drawCircle(MARGIN_LEFT_ITEM+ITEM_WIDTH*i,zeroXY + Math.abs(temperature*y),5,mPaint2);
            }
        }





    }


    public void setStrokenLineBeanList(List<StrokenLineBean> strokenLineBeanList) {
        mStrokenLineBeanList = strokenLineBeanList;
    }
}
